import {
    createRouter,
    createWebHashHistory
} from 'vue-router'

//基本路由组件
import home from "@/pages/index.vue"
import Admin from '@/pages/back/layout/Admin.vue'
import Front from '@/pages/front/layout/Front.vue'
import Login from '@/pages/back/Login.vue'
import NotFound from '@/pages//404.vue'

//动态添加后台管理系统路由组件
import Index from '@/pages/back/index.vue'
import Article from '@/pages/back/article/index.vue'
import Category from '@/pages/back/category/index.vue'
import Tag from '@/pages/back/tag/index.vue'
import Tool from '@/pages/back/tool/index.vue'
import User from '@/pages/back/user/index.vue'


//前台展示路由组件
import BlogList from '@/pages/front/blogList/index.vue'
import BlogSelect from '@/pages/front/blogSelect/index.vue'
import BlogDetail from '@/pages/front/blogDetail/index.vue'
import BlogPerson from '@/pages/front/person/index.vue'
import BlogTool from '@/pages/front/tool/index.vue'
import Home from '@/pages/front/home/index.vue'

//默认路由,由所有用户共享
const routes = [
    {
        path:"/",
        name: "home",
        component: home,
        children: []
    },
    {
        path: '/admin',
        name: 'admin',
        component: Admin,
        children: []
    },
    {
        path: '/front',
        name: 'front',
        component: Front,
        children: [
            {
                path:"/front/home", //这里不能只写成/home
                name:"Home",
                component: Home,
                meta:{
                    title:"博客首页"
                }
            },
            {
                path:"/front/blogList/:param?",
                name:"BlogList",
                component: BlogList,
                meta:{
                    title:"博客列表"
                }
            },
            {
                path:"/front/blogSelect/:param?",
                name:"BlogSelect",
                component: BlogSelect,
                meta:{
                    title:"博客选择"
                }
            },
            {
                path:"/front/tool",
                name:"BlogTool",
                component: BlogTool,
                meta:{
                    title:"博客工具"
                }
            },
            {
                path:"/front/person",
                name:"BlogPerson",
                component: BlogPerson,
                meta:{
                    title:"个人详情"
                }
            },
            {
                path:"/front/blogDetail/:id",
                name:"BlogDetail",
                component: BlogDetail,
                meta:{
                    title:"博客详情"
                }
            },
        ]
    },
    {
        path: '/login',
        component: Login,
        meta: {
            title: '登录页'
        }
    },
    {
        path: '/:pathMatch(.*)*',
        name: 'NotFound',
        component: NotFound
    }
]

//异步添加路由
const asyncRoutes = [
    {
        path:"/index",
        name:"Index",
        component:Index,
        meta:{
            title:"后台首页"
        }
    },
    {
        path:"/article",
        name:"Article",
        component:Article,
        meta:{
            title:"文章管理"
        }
    },
    {
        path:"/category",
        name:"Category",
        component:Category,
        meta:{
            title:"分类管理"
        }
    },
    {
        path:"/tag",
        name:"Tag",
        component:Tag,
        meta:{
            title:"标签管理"
        }
    },
    {
        path:"/tool",
        name:"Tool",
        component:Tool,
        meta:{
            title:"工具管理"
        }
    },
    {
        path:"/user",
        name:"User",
        component:User,
        meta:{
            title:"用户管理"
        }
    },

]


//动态将组件添加到路由中去(注意菜单显示是在FMenu组件中调用了$store.state.menu然后才显示出菜单左侧)
export const router = createRouter({
    history: createWebHashHistory(),
    routes
})

/**
 * 将本来全部可以动态添加的路由和返回回来的路由数据对比,如果返回回来的数据有在本来的动态路由中,则添加到routes中去.
 * @param {Array} menus - 菜单数组
 * @returns {boolean}  是否有新的路由
 */
export function addRoutes(menus){
    let hasNewRoutes = false
    const findAndAddRoutesByMenus = (arr) =>{
        arr.forEach(e=>{
            let item = asyncRoutes.find(o=>o.path === e.path)
            if(item && !router.hasRoute(item.path)){
                const newRoute = {...item, path: `/admin${item.path}`}
                router.addRoute("admin", newRoute)
                hasNewRoutes = true
            }
            if(e.children && e.children.length > 0){
                findAndAddRoutesByMenus(e.children)
            }
        })
    }

    findAndAddRoutesByMenus(menus)

    return hasNewRoutes
}
